//
//  ZZStatusCell.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/16.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWitdth: CGFloat = 35

class ZZStatusCell: UITableViewCell {

    var bottomConstraints: Constraint?
    
    //重写模型的set方法
    var status: ZZStatus? {
        didSet{
            originalView.status = status
            
            //移除约束
            self.bottomConstraints?.uninstall()
            
            if let ret = status?.retweeted_status {
                
                //设置数据源
                retweetedView.retweetedStatus = ret
                //显示
                retweetedView.hidden = false
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }else {
                //隐藏
                retweetedView.hidden = true
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置UI
        setUpUi()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: 设置UI
    private func setUpUi() {
        
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //make.height.equalTo(50)
        }
        
        //转发约束
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        
        //底部转发按钮约束
        bottomView.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.equalTo(retweetedView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
            
        }

        
        //contentView约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    // MARK: 懒加载控件
    private lazy var originalView :ZZStatusOriginalView = ZZStatusOriginalView()
    
    //底部转发按钮
    private lazy var bottomView: ZZStatusCellBottomView = ZZStatusCellBottomView()
    
    //转发微博
    private lazy var retweetedView: ZZReweetedStatusView = ZZReweetedStatusView()
}





